Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[],
    formData:{
      path: '/packageAPI/pages/miniprogramJump/miniprogramJump?zh=wx.restartMiniProgram&title=重启当前小程序&webUrl=https://developers.weixin.qq.com/miniprogram/dev/api/navigate/wx.restartMiniProgram.html',
      url: 'https://mp.weixin.qq.com/s/9O6fTsFrMkp5SDL8kYniiw',
      appId: 'wx5fb16e3de77e43e3',
      miniPath: '',
      shortLink: '',
      extraData: '123',
      envVersion: 'release',
      noRelaunchIfPathUnchanged: false
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    apiName: '',
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
    if(options.zh === 'wx.restartMiniProgram') {
      this.setData({
        schema: [
          {
            label:'打开的页面路径，path 中 ? 后面的部分会成为 query',
            prop:'path',
            type:'text'
          }
        ]
      })
    }
    if(options.zh === 'wx.openOfficialAccountArticle') {
      this.setData({
        schema: [
          {
            label:'需要打开的公众号地址',
            prop:'url',
            type:'text'
          }
        ]
      })
    }
    if(options.zh === 'wx.navigateToMiniProgram') {
      this.setData({
        schema: [
          {
            label:'要打开的小程序 appId',
            prop:'appId',
            type:'text'
          },
          {
            label:'打开的页面路径，如果为空则打开首页',
            prop:'miniPath',
            type:'text'
          },
          {
            label:'需要传递给目标小程序的数据',
            prop:'extraData',
            type:'text'
          },
          {
            label:'要打开的小程序版本',
            prop:'envVersion',
            type:'picker',
            labelOptions:['开发版','体验版','正式版'],
            valueOptions:['develop', 'trial','release'],
          },
          {
            label:'小程序链接，当传递该参数后，可以不传 appId 和 path。链接可以通过【小程序菜单】->【复制链接】获取',
            prop:'shortLink',
            type:'text'
          },
          {
            label:'不reLaunch目标小程序，直接打开目标跳转的小程序退后台时的页面，需满足以下条件：1. 目标跳转的小程序生命周期未被销毁；2. 且目标当次启动的path、query与上次启动相同，apiCategory以wx.getApiCategory接口的返回结果为准',
            prop:'noRelaunchIfPathUnchanged',
            type:'switch'
          },
        ]
      })
    }
  },
  getFormData(e) {
    this.setData({
      formData: e.detail.formData
    })
  },
  restartMiniProgram(e) {
    const that = this;
    wx.restartMiniProgram({
      path: this.data.formData.path,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  openOfficialAccountProfile(e) {
    const that = this;
    wx.openOfficialAccountProfile({
      username: 'gh_68046249a341',
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  openOfficialAccountArticle(e) {
    const that = this;
    wx.openOfficialAccountArticle({
      url: this.data.formData.url,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  exitMiniProgram(e) {
    const that = this;
    wx.exitMiniProgram({
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  navigateToMiniProgram(e) {
    const that = this;
    wx.navigateToMiniProgram({
      appId: this.data.formData.appId,
      path: this.data.formData.miniPath,
      extraData: {
        data: this.data.formData.extraData
      },
      envVersion: this.data.formData.envVersion,
      shortLink: this.data.formData.shortLink,
      noRelaunchIfPathUnchanged: this.data.formData.noRelaunchIfPathUnchanged,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  }
})